package com.binarysoft.service.obj.dao;

import com.binarysoft.service.obj.dao.ObjectProperty;
import com.binarysoft.service.obj.dao.ObjectPropertyKey;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface ObjectPropertyMapper {
    @Delete({
        "delete from object_property",
        "where obj_id = #{objId,jdbcType=INTEGER}",
          "and prop_id = #{propId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(ObjectPropertyKey key);

    @Insert({
        "insert into object_property (obj_id, prop_id, ",
        "prop_name, prop_value, ",
        "is_locked)",
        "values (#{objId,jdbcType=INTEGER}, #{propId,jdbcType=VARCHAR}, ",
        "#{propName,jdbcType=VARCHAR}, #{propValue,jdbcType=VARCHAR}, ",
        "#{isLocked,jdbcType=BIT})"
    })
    int insert(ObjectProperty record);

    @InsertProvider(type=ObjectPropertySqlProvider.class, method="insertSelective")
    int insertSelective(ObjectProperty record);

    @Select({
        "select",
        "obj_id, prop_id, prop_name, prop_value, is_locked",
        "from object_property",
        "where obj_id = #{objId,jdbcType=INTEGER}",
          "and prop_id = #{propId,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="obj_id", property="objId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="prop_id", property="propId", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="prop_name", property="propName", jdbcType=JdbcType.VARCHAR),
        @Result(column="prop_value", property="propValue", jdbcType=JdbcType.VARCHAR),
        @Result(column="is_locked", property="isLocked", jdbcType=JdbcType.BIT)
    })
    ObjectProperty selectByPrimaryKey(ObjectPropertyKey key);

    @UpdateProvider(type=ObjectPropertySqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(ObjectProperty record);

    @Update({
        "update object_property",
        "set prop_name = #{propName,jdbcType=VARCHAR},",
          "prop_value = #{propValue,jdbcType=VARCHAR},",
          "is_locked = #{isLocked,jdbcType=BIT}",
        "where obj_id = #{objId,jdbcType=INTEGER}",
          "and prop_id = #{propId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(ObjectProperty record);
}